<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath()}+'/'">
    <link rel="stylesheet" href="static/element/index.css"/>
</head>
<body>
<div id="app">
    <template>
        <div style="margin-bottom:18px;">
            <el-input style="width: 13%" v-model="queryForm.loginName" placeholder="登录名"></el-input>
            <el-input style="width: 13%" v-model="queryForm.userName" placeholder="姓名"></el-input>
            <el-input style="width: 13%" v-model="queryForm.phone" placeholder="手机号码"></el-input>
            <el-button @click="submitQueryForm">查询</el-button>
            <el-button v-if="button.save" @click="openSaveDialog">新增</el-button>
        </div>

        <el-table :data="tableData" :row-style="{height:'40px'}" :cell-style="{padding:'0px'}" style="font-size: 14px"
                  border>
            <el-table-column type="index" label="序号" width="60"></el-table-column>
            <el-table-column prop="loginName" label="登录名"></el-table-column>
            <el-table-column prop="userName" label="姓名"></el-table-column>
            <el-table-column prop="sex" label="性别" width="100" :formatter="formatSex"></el-table-column>
            <el-table-column prop="birthDate" label="出生日期"></el-table-column>
            <el-table-column prop="phone" label="手机号码"></el-table-column>
            <el-table-column prop="roleName" label="角色" :formatter="formatRole"></el-table-column>
            <el-table-column label="操作" width="160">
                <template slot-scope="scope">
                    <el-button type="text" @click="openDetailDialog(scope.row)">详情</el-button>
                    <el-button v-if="button.update" type="text" @click="openUpdateDialog(scope.row)">修改</el-button>
                    <el-button v-if="button.delete" type="text" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                       :page-sizes="[10, 50, 100]" :page-size="10" layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>

        <el-dialog title="新增" width="90%" :visible.sync="saveDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="saveForm" ref="saveForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="登录名" prop="loginName">
                            <el-input v-model="saveForm.loginName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="saveForm.userName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="性别" prop="sex">
                            <el-radio-group v-model="saveForm.sex">
                                <el-radio :label="1">男</el-radio>
                                <el-radio :label="2">女</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="出生日期" prop="birthDate">
                            <el-date-picker v-model="saveForm.birthDate" type="date" placeholder="选择日期"
                                            style="width:100%"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="手机号码" prop="phone">
                            <el-input v-model="saveForm.phone" maxlength="11"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="省市区" prop="province">
                            <el-cascader v-model="saveForm.province" :options="regionList"
                                         :props="{value:'id', label:'menuName', checkStrictly:true, expandTrigger:'hover', emitPath:false}"
                                         :show-all-levels="false" clearable></el-cascader>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21">
                        <el-form-item label="地址" prop="address">
                            <el-input v-model="saveForm.address"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21">
                        <el-form-item label="角色" prop="roleId">
                            <el-select v-model="saveForm.roleId" multiple placeholder="请选择" style="width:100%">
                                <el-option v-for="item in roleList" :key="item.id" :label="item.roleName"
                                           :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="saveDialog = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="修改" width="90%" :visible.sync="updateDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="updateForm" ref="updateForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="登录名" prop="loginName">
                            <el-input v-model="updateForm.loginName" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="updateForm.userName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="性别" prop="sex">
                            <el-radio-group v-model="updateForm.sex">
                                <el-radio :label="1">男</el-radio>
                                <el-radio :label="2">女</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="出生日期" prop="birthDate">
                            <el-date-picker v-model="updateForm.birthDate" type="date" placeholder="选择日期"
                                            style="width:100%"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="手机号码" prop="phone">
                            <el-input v-model="updateForm.phone" maxlength="11"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="省市区" prop="province">
                            <el-cascader v-model="updateForm.province" :options="regionList"
                                         :props="{value:'id', label:'menuName', checkStrictly:true, expandTrigger:'hover', emitPath:false}"
                                         :show-all-levels="false" clearable></el-cascader>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21">
                        <el-form-item label="地址">
                            <el-input v-model="updateForm.address"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21">
                        <el-form-item label="角色" prop="roleId">
                            <el-select v-model="updateForm.roleId" multiple placeholder="请选择" style="width:100%">
                                <el-option v-for="item in roleList" :key="item.id" :label="item.roleName"
                                           :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="updateDialog = false">取 消</el-button>
                <el-button type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>
    </template>
</div>
</body>
<script src="static/js/axios.min.js"></script>
<script src="static/js/api.js"></script>
<script src="static/js/vue.min.js"></script>
<script src="static/element/index.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
			return {
			    currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                saveDialog: false,
                updateDialog: false,
                button: {
                    save: false,
                    update: false,
                    delete: false
                },
                saveForm: {
                    loginName: '',
                    userName: '',
                    sex: '',
                    birthDate: '',
                    phone: '',
                    roleId: [],
                    province: '',
                    city: '',
                    area: '',
                    address: ''
                },
                updateForm: {
                    id: '',
                    loginName: '',
                    userName: '',
                    sex: '',
                    birthDate: '',
                    phone: '',
                    roleId: [],
                    province: '',
                    city: '',
                    area: '',
                    address: ''
                },
                queryForm: {
                    loginName: '',
                    userName: '',
                    phone: ''
                },
                rules: {
                    loginName: [
                        {required: true, message: '请输入', trigger: 'blur'}
                    ],
                    userName: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    sex: [
                        {required: true, message: '请选择', trigger: 'blur'}
                    ],
                    birthDate: [
                        {required: true, message: '请选择', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 11, max: 11, message: '手机号码格式不正确', trigger: 'blur'}
                    ]
                },
                roleList: [],
                regionList: []
			}
        },
        mounted() {
        },
        methods: {
        	handleCurrentChange(val) {
                this.currentPage = val;
                this.submitQueryForm();
			},
			handleSizeChange(val) {
                this.pageSize = val;
                this.submitQueryForm();
            },
            formatSex: function(row, column, cellValue, index) {
                return row.sex == 1 ? '男' : row.sex == 2 ? '女' : '未知';
            },
            formatRole: function(row, column, cellValue, index) {
                return row.roleName.toString();
            },
            submitQueryForm() {
                let queryParam = {
                    pageNo:this.currentPage,
                    pageSize:this.pageSize,
                    loginName:this.queryForm.loginName,
                    userName:this.queryForm.userName,
                    phone:this.queryForm.phone
                };
                axios.post('sys/user/list', queryParam)
                .then((res) => {
                    if (res.code == '0000') {
                        this.tableData = res.data.list;
                        this.total = res.data.count;
                    }
                });
            },
            openDetailDialog(row) {
            },
            openSaveDialog() {
                this.saveDialog = true;
                if (this.$refs.saveForm !== undefined) {
                    this.$refs.saveForm.resetFields();
                }
                axios.post('sys/role/all')
                .then((res) => {
                    if (res.code == '0000') {
                        this.roleList = res.data;
                    }
                });
            },
            save() {
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.saveDialog = false;
                        let saveParam = this.saveForm;
                        axios.post('sys/user/save', JSON.parse(JSON.stringify(saveParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '新增成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            del(row) {
                this.$confirm('您确定删除吗', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    modal: 'false',
                    type: 'warning'
                }).then(() => {
                    axios.post('sys/user/delete/' + row.id)
                    .then((res) => {
                        if (res.code == '0000') {
                            this.submitQueryForm();
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            });
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                }).catch(() => {
                });
            },
            openUpdateDialog(row) {
                this.updateDialog = true;
                axios.post('sys/role/all')
                .then((res) => {
                    if (res.code == '0000') {
                        this.roleList = res.data;
                    }
                });
                axios.get('sys/user/get/' + row.id)
                .then((res) => {
                    if (res.code == '0000') {
                        this.updateForm.id = res.data.id;
                        this.updateForm.loginName = res.data.loginName;
                        this.updateForm.userName = res.data.userName;
                        this.updateForm.sex = res.data.sex;
                        this.updateForm.birthDate = res.data.birthDate;
                        this.updateForm.phone = res.data.phone;
                        this.updateForm.roleId = res.data.roleId;
                        this.updateForm.province = res.data.province;
                        this.updateForm.city = res.data.city;
                        this.updateForm.area = res.data.area;
                        this.updateForm.address = res.data.address;
                    }
                });
            },
            update() {
                this.$refs.updateForm.validate((valid) => {
                    if (valid) {
                        this.updateDialog = false;
                        let updateParam = this.updateForm;
                        axios.post('sys/user/update', JSON.parse(JSON.stringify(updateParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '修改成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        },
        created() {
			this.submitQueryForm();
			let sysButtons = JSON.parse(localStorage.getItem("jsbb-sysButtons"));
			this.button.save = sysButtons.indexOf("sys/user/save") > -1;
			this.button.update = sysButtons.indexOf("sys/user/update") > -1;
			this.button.delete = sysButtons.indexOf("sys/user/delete") > -1;
        }
    });
</script>
</html>
